package com.exam.service;

import java.sql.SQLException;
import java.util.ArrayList;

import com.exam.dao.GradeDao;
import com.exam.model.GradeModel;
import com.exam.model.TestPaperForm;
import com.exam.util.Tools;

/**
 * 成绩分析 计算
 * @author 刘楠 
 *
 */
public class GradeService {
	
	public static void main(String[] args){
		String str = "EXAMABCDEFG";
		System.out.println(str.substring(4,str.length()));
		for(int i=0 ;i<str.length();i++){
			System.out.println(str.charAt(i));
		}
	}
	
	/**
	 * 保存成绩 返回成绩记录的id。这段解析答案重点看
	 * @param paperForm
	 * @return
	 * @throws SQLException
	 */
	public int saveGrade(TestPaperForm paperForm) throws SQLException{
		
		GradeModel grade = new GradeModel();
		ArrayList<String> qidsList,myAnswerList;
		qidsList = paperForm.getQids();
		myAnswerList = paperForm.getMyAnswer();
		
		
		String qids = qidsList.toString();      //所有试题
		qids = qids.replace("[", "(").replace("]", ")");
		
		String errQids = "( ";   //错误试题
		int  right = 0;  	   //对题数
		String realAnswer = Tools.decodeBase64(paperForm.getRealAnswer());
		realAnswer = realAnswer.substring(4,realAnswer.length());
		
		for(int i=0 ;i<myAnswerList.size() ;i++){
			System.out.println(realAnswer.charAt(i));
			if(myAnswerList.get(i).equals(realAnswer.charAt(i)+"")){  //答案正确
				right++;
			}else{
					errQids += qidsList.get(i)+","; //保存错误的试题
			}
		}
		   errQids += "0 )";
		System.out.println(qids+"======="+errQids);
		grade.setSid(paperForm.getSid());
		grade.setPid(paperForm.getPid());
		grade.setScore(paperForm.getQscore()*right+"");
		grade.setQids(Tools.encodeBase64(qids));
		grade.setErrQid(Tools.encodeBase64(errQids));
		
		System.out.println(grade.toString());
		
		return new GradeDao().addOneGrade(grade); //保存成绩信息 返回id
		
	}
	
}
